library(ri)
library(ri2)
library(ggplot2)
library(fastDummies)
library(AER)
library(gridExtra)

setwd("")


data <- read.csv("Who is the Party - data cleaned for replication archive.csv", na.strings = c("", "NA"))

data <- data[which(data$block != 0),]

data$attentive <- (1 - data$inattentive)

data$attentive[which(data$condition == 3)] <- 0
data$attentive[which(data$condition == 6)] <- 0

#data <- data[which(data$pid7 != 4),]
##Adding in dummies for blocks before spliting the dataset up by condition

data <- dummy_cols(data, select_columns = c("block"))
data <- dummy_cols(data, select_columns = c("pid7"))

##Setting Conditions. 0 = Moderate, 1 = Steroetypical

data$conditionModStereo <- NA
data$conditionModStereo[which(data$condition == 1 | data$condition == 4)] <- 0
data$conditionModStereo[which(data$condition == 2 | data$condition == 5)] <- 1

##Set number of simulations for code
N <- 100000

##First testing H1, about perceived Ideology of out-party

#H1a

#Higher ideology/issue scores are more extreme in the stereotypical direction for that party

data$outIdeo <- NA

data$outIdeo[which(data$condition < 4)] <- data$repIdeo[which(data$condition < 4)]
data$outIdeo[which(data$condition > 3)] <- 1- data$demIdeo[which(data$condition > 3)]

dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$outIdeo) == F & data$inattentive == 0),]

set.seed(621453)

perms <- genperms(dataModStereo$conditionModStereo, blockvar = dataModStereo$block, maxiter = N)

probsModStereo <- genprobexact(dataModStereo$conditionModStereo, blockvar = dataModStereo$block)

pooledIdeo <- omni.ate(dataModStereo$outIdeo, dataModStereo$conditionModStereo, perms = perms,  invert = T, quantiles = c(.05, .95))

#Effect size

pooledIdeoD <- pooledIdeo$ate/(sd(dataModStereo$outIdeo, na.rm = T))

#Repeating this analysis among high-interest respondents for Appendix B

dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$outIdeo) == F & data$inattentive == 0 &
                              data$interest == 1),]

set.seed(730093)

perms <- genperms(dataModStereo$conditionModStereo, blockvar = dataModStereo$block, maxiter = N)

probsModStereo <- genprobexact(dataModStereo$conditionModStereo, blockvar = dataModStereo$block)

pooledIdeo_hiInt <- omni.ate(dataModStereo$outIdeo, dataModStereo$conditionModStereo, perms = perms,  invert = T, quantiles = c(.05, .95))


pooledIdeoD_hiInt <- pooledIdeo_hiInt$ate/(sd(dataModStereo$outIdeo, na.rm = T))


dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$outIdeo) == F & data$inattentive == 0 &
                              data$interest == 0),]

#Repeating this analysis among low-interest respondents for Appendix B

set.seed(865804)

perms <- genperms(dataModStereo$conditionModStereo, blockvar = dataModStereo$block, maxiter = N)

probsModStereo <- genprobexact(dataModStereo$conditionModStereo, blockvar = dataModStereo$block)

pooledIdeo_loInt <- omni.ate(dataModStereo$outIdeo, dataModStereo$conditionModStereo, perms = perms,  invert = T, quantiles = c(.05, .95))

pooledIdeoD_loInt <- pooledIdeo_loInt$ate/(sd(dataModStereo$outIdeo, na.rm = T))

dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$outIdeo) == F & data$inattentive == 0),]

#Using the ANOVA function to conduct an F-Test for HTE by interest

pooledIdeoLMInt <- lm(outIdeo ~ conditionModStereo + interest, data = dataModStereo)

pooledIdeoLMInt_interact <- lm(outIdeo ~ conditionModStereo + interest + conditionModStereo*interest, data = dataModStereo)

anova(pooledIdeoLMInt, pooledIdeoLMInt_interact)

#Taxes

data$outTaxes <- NA

data$outTaxes[which(data$condition < 4)] <- data$repTaxes[which(data$condition < 4)]
data$outTaxes[which(data$condition > 3)] <- 1- data$demTaxes[which(data$condition > 3)]

dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$outTaxes) == F & data$inattentive == 0),]

set.seed(187695)

perms <- genperms(dataModStereo$conditionModStereo, blockvar = dataModStereo$block, maxiter = N)

probsModStereo <- genprobexact(dataModStereo$conditionModStereo, blockvar = dataModStereo$block)

pooledTaxes <- omni.ate(dataModStereo$outTaxes, dataModStereo$conditionModStereo, perms = perms,  invert = T, quantiles = c(.05, .95))

pooledTaxesD <- pooledTaxes$ate/(sd(dataModStereo$outTaxes, na.rm = T))



dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$outTaxes) == F & data$inattentive == 0 &
                              data$interest == 1),]

set.seed(927417)

perms <- genperms(dataModStereo$conditionModStereo, blockvar = dataModStereo$block, maxiter = N)

probsModStereo <- genprobexact(dataModStereo$conditionModStereo, blockvar = dataModStereo$block)

pooledTaxes_hiInt <- omni.ate(dataModStereo$outTaxes, dataModStereo$conditionModStereo, perms = perms,  invert = T, quantiles = c(.05, .95))

pooledTaxesD_hiInt <- pooledTaxes_hiInt$ate/(sd(dataModStereo$outTaxes, na.rm = T))

dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$outTaxes) == F & data$inattentive == 0 &
                              data$interest == 0),]

set.seed(121857)

perms <- genperms(dataModStereo$conditionModStereo, blockvar = dataModStereo$block, maxiter = N)

probsModStereo <- genprobexact(dataModStereo$conditionModStereo, blockvar = dataModStereo$block)

pooledTaxes_loInt <- omni.ate(dataModStereo$outTaxes, dataModStereo$conditionModStereo, perms = perms,  invert = T, quantiles = c(.05, .95))

pooledTaxesD_loInt <- pooledTaxes_loInt$ate/(sd(dataModStereo$outTaxes, na.rm = T))

dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$outTaxes) == F & data$inattentive == 0),]


pooledTaxesLMInt <- lm(outTaxes ~ conditionModStereo + interest, data = dataModStereo)

pooledTaxesLMInt_interact <- lm(outTaxes ~ conditionModStereo + interest + conditionModStereo*interest, data = dataModStereo)

anova(pooledTaxesLMInt, pooledTaxesLMInt_interact)

#Immig

data$outImmig <- NA

data$outImmig[which(data$condition < 4)] <- data$repImmig[which(data$condition < 4)]
data$outImmig[which(data$condition > 3)] <- 1- data$demImmig[which(data$condition > 3)]

dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$outImmig) == F & data$inattentive == 0),]

set.seed(296006)

perms <- genperms(dataModStereo$conditionModStereo, blockvar = dataModStereo$block, maxiter = N)

probsModStereo<- genprobexact(dataModStereo$conditionModStereo, blockvar = dataModStereo$block)

pooledImmig <- omni.ate(dataModStereo$outImmig, dataModStereo$conditionModStereo, perms = perms,  invert = T, quantiles = c(.05, .95))

pooledImmigD <- pooledImmig$ate/(sd(dataModStereo$outImmig))

dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$outImmig) == F & data$inattentive == 0 &
                              data$interest == 1),]

set.seed(488056)

perms <- genperms(dataModStereo$conditionModStereo, blockvar = dataModStereo$block, maxiter = N)

probsModStereo <- genprobexact(dataModStereo$conditionModStereo, blockvar = dataModStereo$block)

pooledImmig_hiInt <- omni.ate(dataModStereo$outImmig, dataModStereo$conditionModStereo, perms = perms,  invert = T, quantiles = c(.05, .95))

pooledImmigD_hiInt <- pooledImmig_hiInt$ate/(sd(dataModStereo$outImmig))

dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$outImmig) == F & data$inattentive == 0 &
                              data$interest == 0),]

set.seed(929049)

perms <- genperms(dataModStereo$conditionModStereo, blockvar = dataModStereo$block, maxiter = N)

probsModStereo <- genprobexact(dataModStereo$conditionModStereo, blockvar = dataModStereo$block)

pooledImmig_loInt <- omni.ate(dataModStereo$outImmig, dataModStereo$conditionModStereo, perms = perms,  invert = T, quantiles = c(.05, .95))

pooledImmigD_loInt <- pooledImmig_loInt$ate/(sd(dataModStereo$outImmig))


dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$outImmig) == F & data$inattentive == 0),]


pooledImmigLMInt <- lm(outImmig ~ conditionModStereo + interest, data = dataModStereo)

pooledImmigLMInt_interact <- lm(outImmig ~ conditionModStereo + interest + conditionModStereo*interest, data = dataModStereo)

anova(pooledImmigLMInt, pooledImmigLMInt_interact)

#Abortion

data$outAbortion <- NA

data$outAbortion[which(data$condition < 4)] <- data$repAbortion[which(data$condition < 4)]
data$outAbortion[which(data$condition > 3)] <- 1- data$demAbortion[which(data$condition > 3)]

dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$outAbortion) == F & data$inattentive == 0),]

set.seed(496446)

perms <- genperms(dataModStereo$conditionModStereo, blockvar = dataModStereo$block, maxiter = N)

probsOneTwo <- genprobexact(dataModStereo$conditionModStereo, blockvar = dataModStereo$block)

pooledAbortion <- omni.ate(dataModStereo$outAbortion, dataModStereo$conditionModStereo, perms, invert = T, quantiles = c(.05, .95))

pooledAbortionD <- pooledAbortion$ate/(sd(dataModStereo$outAbortion))

dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$outAbortion) == F & data$inattentive == 0 &
                              data$interest == 1),]

set.seed(68980)

perms <- genperms(dataModStereo$conditionModStereo, blockvar = dataModStereo$block, maxiter = N)

probsModStereo <- genprobexact(dataModStereo$conditionModStereo, blockvar = dataModStereo$block)

pooledAbortion_hiInt <- omni.ate(dataModStereo$outAbortion, dataModStereo$conditionModStereo, perms = perms,  invert = T, quantiles = c(.05, .95))

pooledAbortionD_hiInt <- pooledAbortion_hiInt$ate/(sd(dataModStereo$outAbortion))

dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$outAbortion) == F & data$inattentive == 0 &
                              data$interest == 0),]

set.seed(683721)

perms <- genperms(dataModStereo$conditionModStereo, blockvar = dataModStereo$block, maxiter = N)

probsModStereo <- genprobexact(dataModStereo$conditionModStereo, blockvar = dataModStereo$block)

pooledAbortion_loInt <- omni.ate(dataModStereo$outAbortion, dataModStereo$conditionModStereo, perms = perms,  invert = T, quantiles = c(.05, .95))

pooledAbortionD_loInt <- pooledAbortion_loInt$ate/(sd(dataModStereo$outAbortion))


dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$outAbortion) == F & data$inattentive == 0),]

pooledAbortionLMInt <- lm(outAbortion ~ conditionModStereo + interest, data = dataModStereo)

pooledAbortionLMInt_interact <- lm(outAbortion ~ conditionModStereo + interest + conditionModStereo*interest, data = dataModStereo)

anova(pooledAbortionLMInt, pooledAbortionLMInt_interact)

#IssueIndex

data$outIssueIndex <- NA

data$outIssueIndex[which(data$condition < 4)] <- data$repIssueIndex[which(data$condition < 4)]
data$outIssueIndex[which(data$condition > 3)] <- 1- data$demIssueIndex[which(data$condition > 3)]

dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$outIssueIndex) == F & data$inattentive == 0),]

set.seed(818610)

perms <- genperms(dataModStereo$conditionModStereo, blockvar = dataModStereo$block, maxiter = N)

probsOneTwo <- genprobexact(dataModStereo$conditionModStereo, blockvar = dataModStereo$block)

pooledIssueIndex <- omni.ate(dataModStereo$outIssueIndex, dataModStereo$conditionModStereo, perms, invert = T, quantiles = c(.05, .95))

pooledIssueIndexD <- pooledIssueIndex$ate/(sd(dataModStereo$outIssueIndex))


dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$outIssueIndex) == F & data$inattentive == 0 &
                              data$interest == 1),]

set.seed(84358)

perms <- genperms(dataModStereo$conditionModStereo, blockvar = dataModStereo$block, maxiter = N)

probsModStereo <- genprobexact(dataModStereo$conditionModStereo, blockvar = dataModStereo$block)

pooledIssueIndex_hiInt <- omni.ate(dataModStereo$outIssueIndex, dataModStereo$conditionModStereo, perms = perms,  invert = T, quantiles = c(.05, .95))

pooledIssueIndexD_hiInt <- pooledIssueIndex_hiInt$ate/(sd(dataModStereo$outIssueIndex))

dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$outIssueIndex) == F & data$inattentive == 0 &
                              data$interest == 0),]

set.seed(11199)

perms <- genperms(dataModStereo$conditionModStereo, blockvar = dataModStereo$block, maxiter = N)

probsModStereo <- genprobexact(dataModStereo$conditionModStereo, blockvar = dataModStereo$block)

pooledIssueIndex_loInt <- omni.ate(dataModStereo$outIssueIndex, dataModStereo$conditionModStereo, perms = perms,  invert = T, quantiles = c(.05, .95))

pooledIssueIndexD_loInt <- pooledIssueIndex_loInt$ate/(sd(dataModStereo$outIssueIndex))


dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$outIssueIndex) == F & data$inattentive == 0),]

pooledIssueIndexLMInt <- lm(outIssueIndex ~ conditionModStereo + interest, data = dataModStereo)

pooledIssueIndexLMInt_interact <- lm(outIssueIndex ~ conditionModStereo + interest + conditionModStereo*interest, data = dataModStereo)

anova(pooledIssueIndexLMInt, pooledIssueIndexLMInt_interact)

#AffectIndex

data$outAffectIndex <- NA

data$outAffectIndex[which(data$condition < 4)] <- data$repAffectIndex[which(data$condition < 4)]
data$outAffectIndex[which(data$condition > 3)] <- data$demAffectIndex[which(data$condition > 3)]

dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$outAffectIndex) == F & data$inattentive == 0),]

set.seed(714466)

perms <- genperms(dataModStereo$conditionModStereo, blockvar = dataModStereo$block, maxiter = N)

probsOneTwo <- genprobexact(dataModStereo$conditionModStereo, blockvar = dataModStereo$block)

pooledAffectIndex <- omni.ate(dataModStereo$outAffectIndex, dataModStereo$conditionModStereo, perms, invert = T, quantiles = c(.05, .95))

pooledAffectIndexD <- pooledAffectIndex$ate/(sd(dataModStereo$outAffectIndex))


dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$outAffectIndex) == F & data$inattentive == 0 &
                              data$interest == 1),]

set.seed(574008)

perms <- genperms(dataModStereo$conditionModStereo, blockvar = dataModStereo$block, maxiter = N)

probsModStereo <- genprobexact(dataModStereo$conditionModStereo, blockvar = dataModStereo$block)

pooledAffectIndex_hiInt <- omni.ate(dataModStereo$outAffectIndex, dataModStereo$conditionModStereo, perms = perms,  invert = T, quantiles = c(.05, .95))

pooledAffectIndex_hiIntD <- pooledAffectIndex_hiInt$ate/(sd(dataModStereo$outAffectIndex))

dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$outAffectIndex) == F & data$inattentive == 0 &
                              data$interest == 0),]

set.seed(543612)

perms <- genperms(dataModStereo$conditionModStereo, blockvar = dataModStereo$block, maxiter = N)

probsModStereo <- genprobexact(dataModStereo$conditionModStereo, blockvar = dataModStereo$block)

pooledAffectIndex_loInt <- omni.ate(dataModStereo$outAffectIndex, dataModStereo$conditionModStereo, perms = perms,  invert = T, quantiles = c(.05, .95))

pooledAffectIndex_loIntD <- pooledAffectIndex_loInt$ate/(sd(dataModStereo$outAffectIndex))

dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$outAffectIndex) == F & data$inattentive == 0),]


pooledAffectIndexLMInt <- lm(outAffectIndex ~ conditionModStereo + interest, data = dataModStereo)

pooledAffectIndexLMInt_interact <- lm(outAffectIndex ~ conditionModStereo + interest + conditionModStereo*interest, data = dataModStereo)

anova(pooledAffectIndexLMInt, pooledAffectIndexLMInt_interact)

#TrustIndex

data$outTrustIndex <- NA

data$outTrustIndex[which(data$condition < 4)] <- data$repTrustIndexRescale[which(data$condition < 4)]
data$outTrustIndex[which(data$condition > 3)] <- data$demTrustIndexRescale[which(data$condition > 3)]

dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$outTrustIndex) == F & data$inattentive == 0),]

set.seed(714466)

perms <- genperms(dataModStereo$conditionModStereo, blockvar = dataModStereo$block, maxiter = N)

probsOneTwo <- genprobexact(dataModStereo$conditionModStereo, blockvar = dataModStereo$block)

pooledTrustIndex <- omni.ate(dataModStereo$outTrustIndex, dataModStereo$conditionModStereo, perms, invert = T, quantiles = c(.05, .95))

pooledTrustIndexD <- pooledTrustIndex$ate/(sd(dataModStereo$outTrustIndex))

dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$outTrustIndex) == F & data$inattentive == 0 &
                              data$interest == 1),]

set.seed(451614)

perms <- genperms(dataModStereo$conditionModStereo, blockvar = dataModStereo$block, maxiter = N)

probsModStereo <- genprobexact(dataModStereo$conditionModStereo, blockvar = dataModStereo$block)

pooledTrustIndex_hiInt <- omni.ate(dataModStereo$outTrustIndex, dataModStereo$conditionModStereo, perms = perms,  invert = T, quantiles = c(.05, .95))

pooledTrustIndex_hiIntD <- pooledTrustIndex_hiInt$ate/(sd(dataModStereo$outTrustIndex))


dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$outTrustIndex) == F & data$inattentive == 0 &
                              data$interest == 0),]

set.seed(771493)

perms <- genperms(dataModStereo$conditionModStereo, blockvar = dataModStereo$block, maxiter = N)

probsModStereo <- genprobexact(dataModStereo$conditionModStereo, blockvar = dataModStereo$block)

pooledTrustIndex_loInt <- omni.ate(dataModStereo$outTrustIndex, dataModStereo$conditionModStereo, perms = perms,  invert = T, quantiles = c(.05, .95))

pooledTrustIndex_loIntD <- pooledTrustIndex_loInt$ate/(sd(dataModStereo$outTrustIndex))


dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$outTrustIndex) == F & data$inattentive == 0),]


pooledTrustIndexLMInt <- lm(outTrustIndex ~ conditionModStereo + interest, data = dataModStereo)

pooledTrustIndexLMInt_interact <- lm(outTrustIndex ~ conditionModStereo + interest + conditionModStereo*interest, data = dataModStereo)

anova(pooledTrustIndexLMInt, pooledTrustIndexLMInt_interact)

#Normalized Feeling Thermometer

data$outThermNorm <- NA

data$outThermNorm[which(data$condition < 4)] <- data$repThermNormRescale[which(data$condition < 4)]
data$outThermNorm[which(data$condition > 3)] <- data$demThermNormRescale[which(data$condition > 3)]

dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$outThermNorm) == F & data$inattentive == 0),]

set.seed(954109)

perms <- genperms(dataModStereo$conditionModStereo, blockvar = dataModStereo$block, maxiter = N)

probsOneTwo <- genprobexact(dataModStereo$conditionModStereo, blockvar = dataModStereo$block)

pooledThermNorm <- omni.ate(dataModStereo$outThermNorm, dataModStereo$conditionModStereo, perms, invert = T, quantiles = c(.05, .95))

pooledThermNormD <- pooledThermNorm$ate/(sd(dataModStereo$outThermNorm))


dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$outThermNorm) == F & data$inattentive == 0 &
                              data$interest == 1),]

set.seed(330046)

perms <- genperms(dataModStereo$conditionModStereo, blockvar = dataModStereo$block, maxiter = N)

probsModStereo <- genprobexact(dataModStereo$conditionModStereo, blockvar = dataModStereo$block)

pooledThermNorm_hiInt <- omni.ate(dataModStereo$outThermNorm, dataModStereo$conditionModStereo, perms = perms,  invert = T, quantiles = c(.05, .95))

pooledThermNormD_hiInt <- pooledThermNorm_hiInt$ate/(sd(dataModStereo$outThermNorm))


dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$outThermNorm) == F & data$inattentive == 0 &
                              data$interest == 0),]

set.seed(47868)

perms <- genperms(dataModStereo$conditionModStereo, blockvar = dataModStereo$block, maxiter = N)

probsModStereo <- genprobexact(dataModStereo$conditionModStereo, blockvar = dataModStereo$block)

pooledThermNorm_loInt <- omni.ate(dataModStereo$outThermNorm, dataModStereo$conditionModStereo, perms = perms,  invert = T, quantiles = c(.05, .95))

pooledThermNormD_loInt <- pooledThermNorm_loInt$ate/(sd(dataModStereo$outThermNorm))


dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$outThermNorm) == F & data$inattentive == 0),]


pooledThermNormLMInt <- lm(outThermNorm ~ conditionModStereo + interest, data = dataModStereo)

pooledThermNormLMInt_interact <- lm(outThermNorm ~ conditionModStereo + interest + conditionModStereo*interest, data = dataModStereo)

anova(pooledThermNormLMInt, pooledThermNormLMInt_interact)


#Social Distance from Out-Party preferences

data$socDistOutIndex <- NA

data$socDistOutIndex[which(data$condition < 4)] <- data$socDistRepsIndex[which(data$condition < 4)]
data$socDistOutIndex[which(data$condition > 3)] <- data$socDistDemsIndex[which(data$condition > 3)]

dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$socDistOutIndex) == F & data$inattentive == 0),]

set.seed(714466)

perms <- genperms(dataModStereo$conditionModStereo, blockvar = dataModStereo$block, maxiter = N)

probsOneTwo <- genprobexact(dataModStereo$conditionModStereo, blockvar = dataModStereo$block)

pooledSocDist <- omni.ate(dataModStereo$socDistOutIndex, dataModStereo$conditionModStereo, perms, invert = T, quantiles = c(.05, .95))

pooledSocDistD <- pooledSocDist$ate/(sd(dataModStereo$socDistOutIndex))


dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$socDistOutIndex) == F & data$inattentive == 0 &
                              data$interest == 1),]

set.seed(420236)

perms <- genperms(dataModStereo$conditionModStereo, blockvar = dataModStereo$block, maxiter = N)

probsModStereo <- genprobexact(dataModStereo$conditionModStereo, blockvar = dataModStereo$block)

pooledSocDist_hiInt <- omni.ate(dataModStereo$socDistOutIndex, dataModStereo$conditionModStereo, perms = perms,  invert = T, quantiles = c(.05, .95))

pooledSocDistD_hiInt <- pooledSocDist_hiInt$ate/(sd(dataModStereo$socDistOutIndex))


dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$socDistOutIndex) == F & data$inattentive == 0 &
                              data$interest == 0),]

set.seed(267287)

perms <- genperms(dataModStereo$conditionModStereo, blockvar = dataModStereo$block, maxiter = N)

probsModStereo <- genprobexact(dataModStereo$conditionModStereo, blockvar = dataModStereo$block)

pooledSocDist_loInt <- omni.ate(dataModStereo$socDistOutIndex, dataModStereo$conditionModStereo, perms = perms,  invert = T, quantiles = c(.05, .95))

pooledSocDistD_loInt <- pooledSocDist_loInt$ate/(sd(dataModStereo$socDistOutIndex))


dataModStereo <- data[which(is.na(data$conditionModStereo) == F & is.na(data$socDistOutIndex) == F & data$inattentive == 0),]

pooledSocDistLMInt <- lm(socDistOutIndex ~ conditionModStereo + interest, data = dataModStereo)

pooledSocDistLMInt_interact <- lm(socDistOutIndex ~ conditionModStereo + interest + conditionModStereo*interest, data = dataModStereo)

anova(pooledSocDistLMInt, pooledSocDistLMInt_interact)

##Now plotting results

#Creating Figure 2, Panel 1

pooledIssueResults <- data.frame(ate = numeric(), lower.ci = numeric(), upper.ci = numeric())

pooledIssueResults[1,] <- c(-pooledIssueIndex$ate, as.numeric(-pooledIssueIndex$conf.intInv[2]), -as.numeric(pooledIssueIndex$conf.intInv[1]))
pooledIssueResults[2,] <- c(-pooledIdeo$ate, as.numeric(-pooledIdeo$conf.intInv[2]), -as.numeric(pooledIdeo$conf.intInv[1]))
pooledIssueResults[3,] <- c(-pooledTaxes$ate, as.numeric(-pooledTaxes$conf.intInv[2]), -as.numeric(pooledTaxes$conf.intInv[1]))
pooledIssueResults[4,] <- c(-pooledImmig$ate, as.numeric(-pooledImmig$conf.intInv[2]), -as.numeric(pooledImmig$conf.intInv[1]))
pooledIssueResults[5,] <- c(-pooledAbortion$ate, as.numeric(-pooledAbortion$conf.intInv[2]), -as.numeric(pooledAbortion$conf.intInv[1]))

rownames(pooledIssueResults) <- c("Issue Index", "Ideology", "Taxes", "Immigration", "Abortion")

pooledIssuePlot <- ggplot(pooledIssueResults, aes(factor(rownames(pooledIssueResults), levels = c("Issue Index", "Ideology", "Taxes", "Immigration", "Abortion")), ate, ymin = lower.ci, ymax = upper.ci)) 
pooledIssuePlot <- pooledIssuePlot + theme(aspect.ratio = 1/2)
pooledIssuePlot <- pooledIssuePlot + geom_pointrange(size = .5, cex = 1) 
pooledIssuePlot <- pooledIssuePlot + geom_hline(yintercept = 0)
pooledIssuePlot <- pooledIssuePlot + ggtitle("Effect of Moderate Exemplars on Perceptions of Outparty Ideology")
pooledIssuePlot <- pooledIssuePlot + theme(plot.title = element_text(size = 10, hjust = .5))
pooledIssuePlot <- pooledIssuePlot + xlab("Issue (90% Confidence Intervals)")
pooledIssuePlot <- pooledIssuePlot + theme(axis.title.x = element_text(size = 8))
pooledIssuePlot <- pooledIssuePlot + ylab("       <- Less Extreme | More Extreme ->")
pooledIssuePlot <- pooledIssuePlot + theme(axis.title.y = element_text(size = 8))
pooledIssuePlot <- pooledIssuePlot + ylim(-.15, .15)

pooledIssuePlot

ggsave("pooledIssuePlot.png", width = 6.5, height = 2.9, units = "in")

#Creating the same figure among high-interest respondents for Appendix B

pooledIssueResults_hiInt <- data.frame(ate = numeric(), lower.ci = numeric(), upper.ci = numeric())

pooledIssueResults_hiInt[1,] <- c(-pooledIssueIndex_hiInt$ate, as.numeric(-pooledIssueIndex_hiInt$conf.intInv[2]), as.numeric(-pooledIssueIndex_hiInt$conf.intInv[1]))
pooledIssueResults_hiInt[2,] <- c(-pooledIdeo_hiInt$ate, as.numeric(-pooledIdeo_hiInt$conf.intInv[2]), as.numeric(-pooledIdeo_hiInt$conf.intInv[1]))
pooledIssueResults_hiInt[3,] <- c(-pooledTaxes_hiInt$ate, as.numeric(-pooledTaxes_hiInt$conf.intInv[2]), as.numeric(-pooledTaxes_hiInt$conf.intInv[1]))
pooledIssueResults_hiInt[4,] <- c(-pooledImmig_hiInt$ate, as.numeric(-pooledImmig_hiInt$conf.intInv[2]), as.numeric(-pooledImmig_hiInt$conf.intInv[1]))
pooledIssueResults_hiInt[5,] <- c(-pooledAbortion_hiInt$ate, as.numeric(-pooledAbortion_hiInt$conf.intInv[2]), as.numeric(-pooledAbortion_hiInt$conf.intInv[1]))

rownames(pooledIssueResults_hiInt) <- c("Issue Index", "Ideology", "Taxes", "Immigration", "Abortion")

pooledIssuePlot_hiInt <- ggplot(pooledIssueResults_hiInt, aes(factor(rownames(pooledIssueResults), levels = c("Issue Index", "Ideology", "Taxes", "Immigration", "Abortion")), ate, ymin = lower.ci, ymax = upper.ci)) 
pooledIssuePlot_hiInt <- pooledIssuePlot_hiInt + theme(aspect.ratio = 1/2)
pooledIssuePlot_hiInt <- pooledIssuePlot_hiInt + geom_pointrange(size = .5, cex = 3) 
pooledIssuePlot_hiInt <- pooledIssuePlot_hiInt + geom_hline(yintercept = 0)
pooledIssuePlot_hiInt <- pooledIssuePlot_hiInt + ggtitle("Effect of Moderate Exemplars on Perceptions of Outparty Ideology\n(High-Interest Respondents)")
pooledIssuePlot_hiInt <- pooledIssuePlot_hiInt + theme(plot.title = element_text(size = 10, hjust = .5))
pooledIssuePlot_hiInt <- pooledIssuePlot_hiInt + xlab("Issue (90% Confidence Intervals)")
pooledIssuePlot_hiInt <- pooledIssuePlot_hiInt + theme(axis.title.x = element_text(size = 8))
pooledIssuePlot_hiInt <- pooledIssuePlot_hiInt + ylab("       <- Less Extreme | More Extreme ->")
pooledIssuePlot_hiInt <- pooledIssuePlot_hiInt + theme(axis.title.y = element_text(size = 8))
pooledIssuePlot_hiInt <- pooledIssuePlot_hiInt + ylim(-.15, .15)

pooledIssuePlot_hiInt

ggsave("pooledIssuePlot_hiInt.png", width = 6.5, height = 2.9, units = "in")

#Creating the same figure among low-interest respondents for Appendix B

pooledIssueResults_loInt <- data.frame(ate = numeric(), lower.ci = numeric(), upper.ci = numeric())

pooledIssueResults_loInt[1,] <- c(-pooledIssueIndex_loInt$ate, as.numeric(-pooledIssueIndex_loInt$conf.intInv[1]), as.numeric(-pooledIssueIndex_loInt$conf.intInv[2]))
pooledIssueResults_loInt[2,] <- c(-pooledIdeo_loInt$ate, as.numeric(-pooledIdeo_loInt$conf.intInv[1]), as.numeric(-pooledIdeo_loInt$conf.intInv[2]))
pooledIssueResults_loInt[3,] <- c(-pooledTaxes_loInt$ate, as.numeric(-pooledTaxes_loInt$conf.intInv[1]), as.numeric(-pooledTaxes_loInt$conf.intInv[2]))
pooledIssueResults_loInt[4,] <- c(-pooledImmig_loInt$ate, as.numeric(-pooledImmig_loInt$conf.intInv[1]), as.numeric(-pooledImmig_loInt$conf.intInv[2]))
pooledIssueResults_loInt[5,] <- c(-pooledAbortion_loInt$ate, as.numeric(-pooledAbortion_loInt$conf.intInv[1]), as.numeric(-pooledAbortion_loInt$conf.intInv[2]))

rownames(pooledIssueResults_loInt) <- c("Issue Index", "Ideology", "Taxes", "Immigration", "Abortion")

pooledIssuePlot_loInt <- ggplot(pooledIssueResults_loInt, aes(factor(rownames(pooledIssueResults), levels = c("Issue Index", "Ideology", "Taxes", "Immigration", "Abortion")), ate, ymin = lower.ci, ymax = upper.ci)) 
pooledIssuePlot_loInt <- pooledIssuePlot_loInt + theme(aspect.ratio = 1/2)
pooledIssuePlot_loInt <- pooledIssuePlot_loInt + geom_pointrange(size = .5, cex = 3) 
pooledIssuePlot_loInt <- pooledIssuePlot_loInt + geom_hline(yintercept = 0)
pooledIssuePlot_loInt <- pooledIssuePlot_loInt + ggtitle("Effect of Moderate Exemplars on Perceptions of Outparty Ideology\n(Low-Interest Respondents)")
pooledIssuePlot_loInt <- pooledIssuePlot_loInt + theme(plot.title = element_text(size = 10, hjust = .5))
pooledIssuePlot_loInt <- pooledIssuePlot_loInt + xlab("Issue (90% Confidence Intervals)")
pooledIssuePlot_loInt <- pooledIssuePlot_loInt + theme(axis.title.x = element_text(size = 8))
pooledIssuePlot_loInt <- pooledIssuePlot_loInt + ylab("       <- Less Extreme | More Extreme ->")
pooledIssuePlot_loInt <- pooledIssuePlot_loInt + theme(axis.title.y = element_text(size = 8))
pooledIssuePlot_loInt <- pooledIssuePlot_loInt + ylim(-.15, .15)

pooledIssuePlot_loInt

ggsave("pooledIssuePlot_loInt.png", width = 6.5, height = 2.9, units = "in")

#Creating Figure 3, Panel 1

pooledAffectResults <- data.frame(ate = numeric(), lower.ci = numeric(), upper.ci = numeric())

pooledAffectResults[1,] <- c(-pooledAffectIndex$ate, as.numeric(-pooledAffectIndex$conf.intInv[1]), as.numeric(-pooledAffectIndex$conf.intInv[2]))
pooledAffectResults[2,] <- c(-pooledSocDist$ate, as.numeric(-pooledSocDist$conf.intInv[1]), as.numeric(-pooledSocDist$conf.intInv[2]))
pooledAffectResults[3,] <- c(-pooledTrustIndex$ate, as.numeric(-pooledTrustIndex$conf.intInv[1]), as.numeric(-pooledTrustIndex$conf.intInv[2]))
pooledAffectResults[4,] <- c(-pooledThermNorm$ate, as.numeric(-pooledThermNorm$conf.intInv[1]), as.numeric(-pooledThermNorm$conf.intInv[2]))

rownames(pooledAffectResults) <- c("Affect Index", "Social Distance", "Trust", "Feeling Therm")

pooledAffectPlot <- ggplot(pooledAffectResults, aes(factor(rownames(pooledAffectResults), levels = c("Affect Index", "Trust", "Feeling Therm", "Social Distance")), ate, ymin = lower.ci, ymax = upper.ci)) 
pooledAffectPlot <- pooledAffectPlot + theme(aspect.ratio = 1/2)
pooledAffectPlot <- pooledAffectPlot + geom_pointrange(size = .5, cex = 3) 
pooledAffectPlot <- pooledAffectPlot + geom_hline(yintercept = 0)
pooledAffectPlot <- pooledAffectPlot + ggtitle("Effect of Moderate Exemplars on Affect Towards Outparty")
pooledAffectPlot <- pooledAffectPlot + theme(plot.title = element_text(size = 10, hjust = .5)) 
pooledAffectPlot <- pooledAffectPlot + xlab("Affect Measure (90% Confidence Intervals)")
pooledAffectPlot <- pooledAffectPlot + theme(axis.title.x = element_text(size = 8))
pooledAffectPlot <- pooledAffectPlot + ylab(NULL)
pooledAffectPlot <- pooledAffectPlot + ylim(-.1, .1)

pooledAffectPlot

ggsave("pooledAffectPlot.png", width = 6.5, height = 2.9, units = "in")

#Creating the same Figure among high-interest respondents for Appendix B

pooledAffectResults_hiInt <- data.frame(ate = numeric(), lower.ci = numeric(), upper.ci = numeric())

pooledAffectResults_hiInt[1,] <- c(-pooledAffectIndex_hiInt$ate, as.numeric(-pooledAffectIndex_hiInt$conf.intInv[1]), as.numeric(-pooledAffectIndex_hiInt$conf.intInv[2]))
pooledAffectResults_hiInt[2,] <- c(-pooledSocDist_hiInt$ate, as.numeric(-pooledSocDist_hiInt$conf.intInv[1]), as.numeric(-pooledSocDist_hiInt$conf.intInv[2]))
pooledAffectResults_hiInt[3,] <- c(-pooledTrustIndex_hiInt$ate, as.numeric(-pooledTrustIndex_hiInt$conf.intInv[1]), min(as.numeric(-pooledTrustIndex_hiInt$conf.intInv[2]), .1))
pooledAffectResults_hiInt[4,] <- c(-pooledThermNorm_hiInt$ate, as.numeric(-pooledThermNorm_hiInt$conf.intInv[1]), as.numeric(-pooledThermNorm_hiInt$conf.intInv[2]))

rownames(pooledAffectResults_hiInt) <- c("Affect Index", "Social Distance", "Trust", "Feeling Therm")

pooledAffectPlot_hiInt <- ggplot(pooledAffectResults_hiInt, aes(factor(rownames(pooledAffectResults_hiInt), levels = c("Affect Index", "Trust", "Feeling Therm", "Social Distance")), ate, ymin = lower.ci, ymax = upper.ci)) 
pooledAffectPlot_hiInt <- pooledAffectPlot_hiInt + theme(aspect.ratio = 1/2)
pooledAffectPlot_hiInt <- pooledAffectPlot_hiInt + geom_pointrange(size = .5, cex = 3) 
pooledAffectPlot_hiInt <- pooledAffectPlot_hiInt + geom_hline(yintercept = 0)
pooledAffectPlot_hiInt <- pooledAffectPlot_hiInt + ggtitle("Effect of Moderate Exemplars on Affect Towards Outparty\n(High-Interest Respondents)")
pooledAffectPlot_hiInt <- pooledAffectPlot_hiInt + theme(plot.title = element_text(size = 10, hjust = .5)) 
pooledAffectPlot_hiInt <- pooledAffectPlot_hiInt + xlab("Affect Measure (90% Confidence Intervals)")
pooledAffectPlot_hiInt <- pooledAffectPlot_hiInt + theme(axis.title.x = element_text(size = 8))
pooledAffectPlot_hiInt <- pooledAffectPlot_hiInt + ylab(NULL)
pooledAffectPlot_hiInt <- pooledAffectPlot_hiInt + ylim(-.1, .1)

pooledAffectPlot_hiInt

ggsave("pooledAffectPlot_hiInt.png", width = 6.5, height = 2.9, units = "in")

#Creating the same Figure among low-interest respondents for Appendix B

pooledAffectResults_loInt <- data.frame(ate = numeric(), lower.ci = numeric(), upper.ci = numeric())

pooledAffectResults_loInt[1,] <- c(-pooledAffectIndex_loInt$ate, as.numeric(-pooledAffectIndex_loInt$conf.intInv[1]), as.numeric(-pooledAffectIndex_loInt$conf.intInv[2]))
pooledAffectResults_loInt[2,] <- c(-pooledSocDist_loInt$ate, as.numeric(-pooledSocDist_loInt$conf.intInv[1]), as.numeric(-pooledSocDist_loInt$conf.intInv[2]))
pooledAffectResults_loInt[3,] <- c(-pooledTrustIndex_loInt$ate, as.numeric(-pooledTrustIndex_loInt$conf.intInv[1]), as.numeric(-pooledTrustIndex_loInt$conf.intInv[2]))
pooledAffectResults_loInt[4,] <- c(-pooledThermNorm_loInt$ate, as.numeric(-pooledThermNorm_loInt$conf.intInv[1]), as.numeric(-pooledThermNorm_loInt$conf.intInv[2]))


rownames(pooledAffectResults_loInt) <- c("Affect Index", "Social Distance", "Trust", "Feeling Therm")

pooledAffectPlot_loInt <- ggplot(pooledAffectResults_loInt, aes(factor(rownames(pooledAffectResults_loInt), levels = c("Affect Index", "Trust", "Feeling Therm", "Social Distance")), ate, ymin = lower.ci, ymax = upper.ci)) 
pooledAffectPlot_loInt <- pooledAffectPlot_loInt + theme(aspect.ratio = 1/2)
pooledAffectPlot_loInt <- pooledAffectPlot_loInt + geom_pointrange(size = .5, cex = 3) 
pooledAffectPlot_loInt <- pooledAffectPlot_loInt + geom_hline(yintercept = 0)
pooledAffectPlot_loInt <- pooledAffectPlot_loInt + ggtitle("Effect of Moderate Exemplars on Affect Towards Outparty\n(Low-Interest Respondents)")
pooledAffectPlot_loInt <- pooledAffectPlot_loInt + theme(plot.title = element_text(size = 10, hjust = .5)) 
pooledAffectPlot_loInt <- pooledAffectPlot_loInt + xlab("Affect Measure (95% Confidence Intervals)")
pooledAffectPlot_loInt <- pooledAffectPlot_loInt + theme(axis.title.x = element_text(size = 8))
pooledAffectPlot_loInt <- pooledAffectPlot_loInt + ylab(NULL)
pooledAffectPlot_loInt <- pooledAffectPlot_loInt + ylim(-.1, .1)

pooledAffectPlot_loInt

ggsave("pooledAffectPlot_loInt.png", width = 6.5, height = 2.9, units = "in")

